package com.mmall.pay.provider.controller;

import com.mmall.common.spring.security.LoginRequired;
import com.mmall.common.spring.security.UserSecurityContextHolder;
import com.mmall.order.api.service.OrderService;
import com.mmall.pay.api.bo.PayInfoBO;
import com.mmall.pay.api.dto.PayApplyDTO;
import com.mmall.pay.api.dto.PayCallbackDTO;
import com.mmall.pay.api.service.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * @author hejianglong
 * @date 2019/10/28
 */
@RestController
@RequestMapping("/pay")
@Validated
public class PayController {

    @Autowired
    private PayService payService;

    /**
     * 返回支付信息
     * @param orderId
     * @return
     */
    @GetMapping("/info")
    @LoginRequired
    public PayInfoBO info(@NotNull Long orderId) {
        return payService.needPayInfo(orderId);
    }

    /**
     * 申请支付
     * @param payApplyDTO
     */
    @PostMapping("/payApply")
    @LoginRequired
    public void payApply(@Valid PayApplyDTO payApplyDTO) {
        Long userId = UserSecurityContextHolder.getContext().getUserId();
        payApplyDTO.setUserId(userId);
        payService.payApply(payApplyDTO);
    }


    /**
     * 支付成功第三方回调
     * 模拟调用非真实场景
     * @param payCallbackDTO
     */
    @PostMapping("/payCallback")
    public void payCallback(@Valid PayCallbackDTO payCallbackDTO) {
        payService.payCallback(payCallbackDTO);
    }
}
